﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Cisoft.Cisoft3G.Model;
using Cisoft.Cisoft3G.BLL;

public partial class Business_Cisoft3G_ContactsManage_CallContacts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindTree(0,(TreeNode)null);
        }
    }


    #region 数据绑定
    /// <summary>
    /// 数据绑定
    /// </summary>
    private void Bind()
    {
        //当前页码
        int cpn = AspNetPager1.CurrentPageIndex;
        //获取数据
        List<ContactsMO> modelList = new ContactsBO().GetModelList(" CG_ContactGroupID_FK = " + tvContactsGroup.SelectedValue);

        int pageNum = modelList.Count;
        if (modelList.Count > 0)
        {
            Repeater1.DataSource = modelList;
            Repeater1.DataBind();
            AspNetPager1.RecordCount = pageNum;
            AspNetPager1.PageSize = 10;
        }
        else
        {
            Repeater1.DataSource = modelList;
            Repeater1.DataBind();
            AspNetPager1.RecordCount = pageNum;
        }
    }
    #endregion

    #region 绑定TreeView数据
    //绑定TreeView数据
    private void BindTree(int pid,TreeNode node)
    {
        List<ContactGroupMO> modelList = new ContactGroupBO().GetModelList(" CG_ParentID = " + pid + " order by CG_Order desc");
        foreach (ContactGroupMO model in modelList)
        {
            TreeNode tn = new TreeNode();
            if (node == null)
            {
                tn = new TreeNode(model.CG_GroupName, model.CG_ContactGroupID.ToString());
                tvContactsGroup.Nodes.Add(tn);
                BindTree(model.CG_ContactGroupID,tn);
            }
            else
            {
                tn = new TreeNode(model.CG_GroupName, model.CG_ContactGroupID.ToString());
                node.ChildNodes.Add(tn);
                BindTree(model.CG_ContactGroupID, tn);
            }
        }
    }
    #endregion

    #region TreeView选择项改变
    protected void tvContactsGroup_SelectedNodeChanged(object sender, EventArgs e)
    {
        Bind();
    }
    #endregion

    #region 翻页事件
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind();
    }
    #endregion

    #region 选择联系人事件
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        
    }
    #endregion

    #region 选择框选择事件
    protected void Check_Click(object sender, EventArgs e)
    {
        CheckBox cb = sender as CheckBox;
        if (cb.Checked)
        {
            
            Label lblContactID = cb.Parent.FindControl("lblContactID") as Label;
            ContactsMO model = new ContactsBO().GetModel(int.Parse(lblContactID.Text));
            if (model != null)
            {
                string strContact = "<" + model.CT_Name + ">" + model.CT_Mobile + ",";

                if (!txtContactsSelected.Text.Contains(strContact))
                {
                    txtContactsSelected.Text += strContact;
                }
            }
        }
    }
    #endregion

    #region 查询事件
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        //当前页码
        int cpn = AspNetPager1.CurrentPageIndex;
        //获取数据
        List<ContactsMO> modelList = new ContactsBO().GetModelList(GetQueryString());

        int pageNum = modelList.Count;
        if (modelList.Count > 0)
        {
            Repeater1.DataSource = modelList;
            Repeater1.DataBind();
            AspNetPager1.RecordCount = pageNum;
            AspNetPager1.PageSize = 10;
        }
        else
        {
            Repeater1.DataSource = modelList;
            Repeater1.DataBind();
            AspNetPager1.RecordCount = pageNum;
        }
    }
    #endregion

    #region 获得查询条件
    private string GetQueryString()
    {
        string strQuery = " 1=1 ";
        if (!string.IsNullOrEmpty(txtName.Text.Trim()))
        {
            strQuery += " and CT_Name like '%" + txtName.Text.Trim() + "%' ";
        }
        if (!string.IsNullOrEmpty(txtMobile.Text.Trim()))
        {
            strQuery += " and CT_Mobile like '%" + txtMobile.Text.Trim() + "%' ";
        }

        return strQuery;
    }
    #endregion

    #region 全选事件
    protected void chcAll_CheckedChanged(object sender, EventArgs e)
    {
        if (chcAll.Checked)
        {
            foreach (RepeaterItem item in Repeater1.Items)
            {
                CheckBox cb = item.FindControl("cbk_single") as CheckBox;
                cb.Checked = true;
                if (cb.Checked)
                {
                    Label lblContactID = item.FindControl("lblContactID") as Label;
                    ContactsMO model = new ContactsBO().GetModel(int.Parse(lblContactID.Text));
                    if (model != null)
                    {
                        string strContact = "<" + model.CT_Name + ">" + model.CT_Mobile + ",";

                        if (!txtContactsSelected.Text.Contains(strContact))
                        {
                            txtContactsSelected.Text += strContact;
                        }
                    }
                }
            }
        }
        else
        {
            foreach (RepeaterItem item in Repeater1.Items)
            {
                CheckBox cb = item.FindControl("cbk_single") as CheckBox;
                cb.Checked = false;
            }
        }
    }
    #endregion
    
}
